package com.wing.contract.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.contract.model.entity.WarehouseContract;
import com.wing.contract.model.request.WarehouseContractForm;
import com.wing.contract.model.response.WarehouseContractVO;
import com.wing.user.service.WarehouseContractService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 托管（云仓）合同(warehouse_contract)表控制层
 *
 * @author xxxxx
 */
@Api(value = "生产合同管理", tags = "#/warehouse/contract/produceContract")
@RestController("WarehouseProduceContractController")
@RequestMapping("/warehouse/produceContract")
@RequiredArgsConstructor
public class WarehouseProduceContractController {
    /**
     * 服务对象
     */
    @Autowired
    private WarehouseContractService warehouseContractService;


    @ApiOperation(value = "查询所有合同", notes = "搜索")
    @GetMapping("/list")
    public PageResult<WarehouseContractVO> list(
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page
            ,@RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(value = "sort", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(value = "order", defaultValue = "asc", required = false) String order
            ,String warehouseCustomerId
            ,Integer type
    ){

        Page<WarehouseContract> pg = new Page<>(page, limit);

        Map paramMap=new HashMap();
        if(StringUtils.isNotEmpty(warehouseCustomerId)){
            paramMap.put("warehouseCustomerId",warehouseCustomerId);
        }
        paramMap.put("type",type);
        //搜索
        IPage<WarehouseContractVO> list =warehouseContractService.pageList4app(pg,paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
//        return warehouseContractService.pageList4app(pg,paramMap);
    }

    @ApiOperation(value = "添加合同", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@RequestBody WarehouseContractForm warehouseContractForm)  {
        try{

            if(warehouseContractService.saveProduce(warehouseContractForm)){
                return JsonResult.success("添加成功");
            }else {
                return JsonResult.fail("添加失败");
            }
        }catch (Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "详情", notes = "详情")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @GetMapping("/info")
    public JsonResult info(Long id ) {
        return JsonResult.success(warehouseContractService.info(id));
    }

}
